


function  jper_xr(input_folder,output_folder,compressed_folder)

%I have to compressed the images according to desired compression ratio.
%But the the problem is that there is not compession parameter to compress
%direclty. So What I did is that I controlled the compression ratio by
%changing the quality of the image. More the quality of image, less is the
%compression and vice versa.

% I started with random quality e.g 55 and then increased or decreased the
% quality depending on the compression requirement.

% May be my code is not optimized but atleast it works correctly..   :)

% Input Folder
 a=input_folder; 
  
 c=compressed_folder;
 
 du=output_folder;
 
 d=dir([a '*.bmp'])

 mkdir(du)
 temp_folder=tempdir;
 t=dir([c '*.jxr'])     
 
    for i=[1:numel(d)]
        org=imread([a d(i).name]);

        img_new_name= strrep([du d(i).name],'.bmp','.jxr');
      



       % org=imread('004.tiff');


       f=50;

       system( ['JXREncApp.exe -i "',a , d(i).name,'" -o "',img_new_name, '"  -q ' , num2str(f)] ); 




       % org=imread('004.tiff');
       % imwrite(org,img_new_name, 'jpg' , 'quality' , 55);
        K=dir(img_new_name);
        %K=imfinfo('ab.jpg');


        K1=dir([c t(i).name]);

        % if image is already compressed then    
        %image_byte = K1.FileSize;

        % If Image is not compressed then size of uncompressed Image
        image_byte = K1.bytes;
        %Size of compressed Image

        compressed_byte=K.bytes;
        CR =  image_byte /compressed_byte;




        % as we need required compression so that we can set the value of CR
        % accordingly. As my case I need compression 10. So my CR is between 11
        % and 10. If we want to make CR=10 then we hvae to decrease the step
        % size and then matlab will take a lot of time.

        if CR > 10
            for f=[1:1:49]
                 if CR > 1.01  

                      system( ['JXREncApp.exe -i "',a , d(i).name,'" -o "',img_new_name, '"  -q ' , num2str(50-f)] );

                     K=dir(img_new_name)

                    compressed_byte=K.bytes;


                    CR =  image_byte /compressed_byte


                 end
            end 
        end 
        
      if CR < 10
          for f=[1:1:49]
                 if CR < 1.01  

                      system( ['JXREncApp.exe -i "',a , d(i).name,'" -o "',img_new_name, '"  -q ' , num2str(50+f)] );

                     K=dir(img_new_name);

                    compressed_byte=K.bytes;


                    CR =  image_byte /compressed_byte;


                 end
            end
          
          
      end 





        

    end 
 end 

    
    

